﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqModule
    {
        public List<Module> GetModules(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<ModuleEntity> query = db.ModuleEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "ModuleId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.ModuleId);
                            else
                                query = query.OrderByDescending(c => c.ModuleId);
                            break;
                        case "ModuleName":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.ModuleName);
                            else
                                query = query.OrderByDescending(c => c.ModuleName);
                            break;

                    }
                }
                return query.Select(c => Mapper.ToModuleObject(c)).ToList();
            }
        }
    }
}
